package graphics;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import physics.World;

/**
 * takes a world and displays the given world in a new frame;
 * 
 * @author Thomas
 *
 */
public class WorldDisplayer extends JPanel{
World world;
BufferedImage img;
private final int FPS = 30;
	public WorldDisplayer(World world){
		this.world = world;
		
		setMinimumSize(new Dimension(800,800));
		setMaximumSize(new Dimension(1900,1080));
		setPreferredSize(ComponentFrame.getScreenSize());
		setSize(getPreferredSize());
		repaint();
		Timer t = new Timer(1000/FPS, new ActionListener(){



			//timer to set when to draw the screen and update values
			@Override
			public void actionPerformed(ActionEvent arg0) {
				repaint();
			}

		});
		t.start();
		world.start();
		ComponentFrame.showFrame("test", this);
	}
	
	
	
	
	@Override
	public void paint(Graphics g){
		//img =(BufferedImage) createImage(this.getWidth(),this.getHeight());
		img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2D = (Graphics2D) img.createGraphics();
		g2D.setColor(Color.WHITE);
		g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
		WorldDrawer.drawWorld(g2D, world);
		g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
	}
	
	@Override
	public void update(Graphics g){
		paint(g);
	}
	
	
	
}
